package com.xiaocheng.www.controller;

import com.xiaocheng.www.common.utils.http.AxiosResult;
import com.xiaocheng.www.common.utils.http.AxiosStatus;
import com.xiaocheng.www.common.utils.page.Page;
import com.xiaocheng.www.dto.impl.BrandDto;
import com.xiaocheng.www.entiy.impl.Brand;

import com.xiaocheng.www.querry.impl.BrandQuerry;
import com.xiaocheng.www.service.BrandService;
import com.xiaocheng.www.transfer.impl.BrandTransfer;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
@RequiredArgsConstructor
@CrossOrigin(origins = "*", methods = {RequestMethod.DELETE, RequestMethod.PUT,RequestMethod.POST,RequestMethod.GET})
public class BrandController {

    public final BrandService brandService;

   public  final BrandTransfer brandTransfer;


    /**
     * 分页 条件查询
     * @param brandQuerry
     * @return
     */
    @GetMapping()
    public AxiosResult<List<BrandDto>> getAllBrand(BrandQuerry brandQuerry){

        Page<BrandDto> page = brandService.pageFind(brandQuerry);

        return  AxiosResult.success(AxiosStatus.OK,page);
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public AxiosResult<BrandDto> getById(@PathVariable Long id){
        Brand byId = brandService.findById(id);
        return AxiosResult.success(AxiosStatus.OK,byId);
    }

    /**
     * 添加
     * @param brand
     * @return
     */
    @PostMapping
    public AxiosResult<Void> add(@RequestBody Brand brand){
        int i = brandService.addId(brand);

        return  i > 0 ? AxiosResult.success() : AxiosResult.error();
    }

    /**
     * 修改
     * @param t
     * @return
     */
    @PutMapping()
    public AxiosResult<Void> update(@RequestBody Brand brand){
        int i = brandService.updateId(brand);
        return  i > 0 ? AxiosResult.success() : AxiosResult.error();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("del/{id}")
    public AxiosResult<Void> delete(@PathVariable Long id){
        int i = brandService.deleteById(id);
        return  i > 0 ? AxiosResult.success() : AxiosResult.error();
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("dels/{ids}")
    public AxiosResult<Void> delete(@PathVariable List<Long> ids){
        int i = brandService.deleteByIds(ids);
        return  i > 0 ? AxiosResult.success() : AxiosResult.error();
    }

}
